﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using TanWeb.Models.Entities;
using TanWeb.Models.DataAccess.Infrastructure;
using TanWeb.Helpers;

namespace TanWeb.Models.DataAccess
{
    public class ItemGroupDataAccess : BaseDataAccess
    {
        //===============================================================
        //  コンストラクタ等宣言
        //===============================================================
        #region /***    Constructor Destructor Declared    ***/

        /// <summary>
        /// コンストラクタ
        /// </summary>
        /// <param name="dbContext">データベースコンテキスト</param>
        public ItemGroupDataAccess(DbContext dbContext) : base(dbContext) { }

        public List<ItemGroup> GetAllItemGroup()
        {
            // ログを記述する
            logger.Info("\t\tBEGIN DataAccess: ItemGroupDataAccess; Action GetAllItemGroup");

            List<ItemGroup> lstItemGroup = new List<ItemGroup>();
            string sql = string.Empty;

            try
            {
                sql = @"SELECT * 
                      FROM ItemGroup 
                      WHERE del_flg = 0
                      ORDER BY id DESC";
                lstItemGroup = this.DbContext.Database.SqlQuery<ItemGroup>(sql).ToList();
            }
            catch (Exception ex)
            {
                logger.Error("\t\tError DataAccess: ItemGroupDataAccess; Action GetAllItemGroup", ex);
            }

            logger.Info("\t\tEnd DataAccess: ItemGroupDataAccess; Action GetAllItemGroup");

            return lstItemGroup;
        }

        /*
        public List<SubItemGroup> GetSubGroup(string itemGroupId)
        {
            List<SubItemGroup> lstSubItemGroup = new List<SubItemGroup>();
            // ログを記述する
            logger.Info("\t\tBEGIN DataAccess: ItemGroupDataAccess; Action GetAllItemGroup");
            string sql = string.Empty;
            string condition = string.Empty;

            try
            {
                if (itemGroupId != null && itemGroupId != string.Empty && itemGroupId != "0")
                {
                    condition = string.Format("AND groupId = {0}", itemGroupId);
                }

                sql = string.Format(
                    @"SELECT * 
                      FROM SubItemGroup 
                      WHERE del_flg = 0
                        {0}
                      ORDER BY id DESC", condition);

                lstSubItemGroup = this.DbContext.Database.SqlQuery<SubItemGroup>(sql).ToList();
            }
            catch (Exception ex)
            {
                logger.Error("Error DataAccess: ItemGroupDataAccess; Action GetAllItemGroup", ex);
            }

            logger.Info("\t\tEnd DataAccess: ItemGroupDataAccess; Action GetAllItemGroup");

            return lstSubItemGroup;
        }
         * */

        public string RegisterGroup(string groupName)
        {
            // ログを記述する
            logger.Info("\t\tBEGIN DataAccess: ItemGroupDataAccess; Action RegisterGroup");
            string sql = string.Empty;
            string date = DateTime.Now.ToString(Constants.FORMAT_FULL_DATE_TIME);
            int result = 0;

            try
            {
                sql = string.Format(
                    @"INSERT INTO ItemGroup
                    VALUES (N'{0}', 0, '{1}')"
                    , groupName, date);

                result = this.DbContext.Database.ExecuteSqlCommand(sql);
            }
            catch (Exception ex)
            {
                logger.Error("\t\tError DataAccess: ItemGroupDataAccess; Action RegisterGroup", ex);
            }

            logger.Info("\t\tEnd DataAccess: ItemGroupDataAccess; Action RegisterGroup");

            return result.ToString();
        }

        #endregion

        
    }
}